// ==UserScript==
// @name 139.work
// @author wgu
// @description  139life
// @include http://www.139life.com/forum.php*
// ==/UserScript==
gw.import('yzm.js',window);

(function($){
	var useInfo=[
		[0,'15850116859','456258','legu2009',0]
		,[0,'13584865387','456258','lincheng',0]
		,[0,'13405076133','456258','legu520',0]
		,[0,'13812646820','109798','wxiao',0]
		,[0,'15050125800','861231','lock2009']
		,[0,'15952415129','870928','locker520']
	];
	
	var pref = gw.pref('wgu.139life.'),cache=gw.cache,ob=gw.ob;
	var req = new XMLHttpRequest();
	var req2 = new XMLHttpRequest();
	req2.addEventListener("load", function () {
			if (this.readyState == 4) { 
					document.querySelectorAll("#seccode_"+document.getElementsByName("sechash")[0].value+" .vm")[0].src=this.responseText.match(/src="([^"]+)/)[1];
			}
	}, false);
	
	var updateseccode=window.updateseccode=function(s){
		req2.open('GET', 'http://www.139life.com/misc.php?mod=seccode&action=update&idhash='+s, true); 
		req2.send();
	}

	cache['139life']=cache['139life']||0;
	if(cache['139life']==0){	
		(function(){
			for(var i=0;i<useInfo.length;i++){
				gw.cookie.remove('.139life.com','/');
				req.open('POST', 'http://www.139life.com/member.php?mod=logging&action=login&loginsubmit=yes&floatlogin=yes&inajax=1', false); 
				req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				req.send('username='+useInfo[i][1]+'&password='+useInfo[i][2]);
				req.open('GET', 'http://www.139life.com/forum.php?mod=viewthread&tid=253129&extra=page%3D8', false); 
				req.send(null);
				pref.setValue('fromhash'+i,req.responseText.match(/formhash[^"]+/g)[0]);
				pref.setValue('cookie'+i,gw.cookie.get('.139life.com','/'));
			}
		})();
		for(i=0;i<useInfo.length;i++){
			console.log(",['"+pref.getValue('fromhash'+i)+"','"+pref.getValue('cookie'+i)+"']");
		}	
		cache['139life']=1;
	}
	
	if($('#um strong').length==0){
		window.location.reload();
		return;
	}

	function fn (){
		gw.cookie.remove('.139life.com','/');
		gw.cookie.add('.139life.com','/',pref.getValue('cookie'+this.value));
		
		$("[name='formhash']").val(pref.getValue('fromhash'+this.value).replace('formhash=',''))
		
		if($(".mtm").length!=0){
			updateseccode($("[name='sechash']").val());
		}
	}
	
	(function(){
		var p=$('#um strong'),x=p.text();
		var html='',i=0,l=useInfo.length;
		for(i=0;i<l;i++){
			if(x!=useInfo[i][3]){
				html+='<option value="'+i+'">'+useInfo[i][3]+'</option>';
			}else{
				html='<option value="'+i+'">'+useInfo[i][3]+'</option>'+html;
			}
		}
		$('<select id="wgu_sel" >'+html+'</select>').insertBefore(p).bind('change',fn);
	})();
		
	
	if(!document.getElementById("fastpostform")){
		return ;
	}

	window._oldshowMenu=window.showMenu;
	window.showMenu=function (attr, mtype){
		var sechash=document.getElementsByName("sechash").length>0?document.getElementsByName("sechash")[0].value:'';
		if(attr=='seccode'+sechash){
			return;
		}else{
			_oldshowMenu(attr, mtype);
		}
	}

	window._oldhideMenu=window.hideMenu;
	window.hideMenu=function (attr, mtype){
		var sechash=document.getElementsByName("sechash").length>0?document.getElementsByName("sechash")[0].value:'';
		if(attr=='seccode'+sechash+'_menu'){
			return;
		}else{
			_oldhideMenu(attr, mtype);
		}
	}
	
	var code={
		'0@0:1-0:1-00-01:1-01@0@0:1-0:10-11-0:10-11':'2'
		,'0:1-2:2:3:4@0:1-0:2-3-0:4-0@0:1:2:3-2:4@0:1-0:2-3-0:4-0':'2'
		,'0@0:1-0:1-2-0:1:10-11@0@0:00-01-02:00-02':'3'
		,'0:1-2:3:4:5-4:6@0:1-0:2-3:2-4-3:5-6@0:1-2:2:3:4-5:6@0:1-0:2-3-4:2-4:5-6':'3'
		,'0:0-1:0@0@0:00-01:00@0':'4'
		,'0:1-0:1@0:1-0:1@0:00-01:01@0:00-01:01':'4'
		,'0:1:1-2:3@0:1-0-2:3@0:1-2:1:3@0:1-2-3:2':'6'
		,'0:00-01:00@0:00-01-02:01-02:01@0:00-01:00@0:00-01:1-00-01:1':'6'
		,'0@0:0-1:0@0@0:00-01:00':'7'
		,'0:1@0:0-1:0@0:1@0:0-1:0':'7'
		,'0:00-01:00:000-001:000@0:00-01:000-001-01:000-01:000@0:00-01:00:000-001:000@0:00-01:000-001-01:000-01:000':'8'
		,'0:1-2:3:4-5:6@0:1-0:2-3-4:5-6@0:1-2:3:4-5:6@0:1-0:2-3-4:5-6':'8'
		,'0:1-2:2:3-2:4@0:1-0:2-3-4:3@0:1-2:2:3-2:4@0:1-0-2:3-4':'9'
		,'0:00-01:00@0:00-01:00-01-1:00@0:00-01:00@0:00-01-02:00-01:00':'9'
		,'0:0-1:0:0-2:0@0:00-01-02:1-2@0:0-1:0:0-2:0@0:1-0:2-3-4:2':'b'
		,'0:00-01:00:000-001:000@0:00-01-02:00:000-001@0:00-01:00:000-001:000@0:00-01-02:00':'b'
		,'0@0:00-01@0@0:1-0:1':'c'
		,'0:1-2:1:1-3:4@0:1-2:3-4@0:1-2:1:1-3:4@0:1-0:2-3:4':'c'
		,'0@0:00-01-02:00-02@0@0:1-0:1-2-0:1':'e'
		,'0@0:00-01:00@0@0:0-1:0':'f'
		,'0:1-2:1:1-3:3@0:1-2:1-3-2:4-3@0:1-0:1:1-2:3@0:1-0:2-00-01:2-01:3':'g'
		,'0:0-1:0@0:00-01:00-1-01:00-1@0:00-01:00@0:1-0:1-00-01:1-01:1':'g'
		,'0:1-0:1:10-11@0@0:1-0:1:10-11@0':'h'
		,'0:1-0:1@0@0:00-01:01@0':'j'
		,'0:1-0:1:10-11@0:00-01@0:1-0:1:10-11@0:1-0:1':'k'
		,'0:1-0:1:10-11-12:10-12@0@0:1-0:1-2-0:1:10-11@0':'m'
		,'0:1-0:1-2-3-0:1-4-0@0:1:2:3:4@0:1-2-0:1-3-4-0:1-0@0:1:2:3:4':'m'
		,'0:00-01:00@0:00-01:00@0:00-01:00@0:00-01:00':'p'
		,'0:0-1:0@0:00-01:1@0:0-1:0@0:1-2:1':'p'
		,'0:00-01:00-1-01:00-1:00:000-001@0:00-01:00-1-01:00-1@0:00-01:00-010-011:00-011:00@0:1-0:10-11:10-110-111:10-111:10':'q'
		,'0:1-2:1-3-2:1-4:5-6@0:1-2:1-3-2:1-4:5-6@0:1-0:2-3:2-4-5:2-5:6@0:1-0:2-3:2-4-5:2-5:6':'q'
		,'0:00-01:00-1-01:00-1:00:000-001@0:00-01:00-1-01:00-1:00:000-001@0:00-01:00-010-011:00-011:00@0:00-01:00-010-011:00-011:00':'q'
		,'0:0-1:0:0-2@0:00-01:1-2@0:1-0:1:1-2:1@0:1-0:2-3:2':'r'
		,'0:00-01:00:000-001@0:00-01:00-010-011:00-011@0:1-0:1:10-11:10@0:1-0:10-11-0:10-11:10':'r'
		,'0@0@0@0':'t'
		,'0:00-01-02:01@0@0:1-0-2:1@0':'t'
		//,'0:1-0:1@0@0:00-01@0':'y'
		//,'0:1-0:2-3:4@0:1:2:3:4@0:1-2:3-4@0:1:2:3:4':'y'
		,'0:1-0:1-2-0:1@0@0:00-01-02:00-02@0':'w'
		,'0:1-0:2-3-4:5-6@0:1:2:3:4:5:6@0:1-0:2-3-4:5-6@0:1:2:3:4:5:6':'w'
		,'0:1-0:1:10-11@0:1-0:1:10-11@0:1-0:1:10-11@0:1-0:1:10-11':'x'
		,'0:1-0:2-3:4:5-6:7-8@0:1-0:2-3:4:5-6:7-8@0:1-0:2-3:4:5-6:7-8@0:1-0:2-3:4:5-6:7-8':'x'
	};
	
	function erro(){
		console.log('error');
		document.getElementsByName("seccodeverify")[0].value='';
		updateseccode(document.getElementsByName("sechash")[0].value);
	}
	
	$.yzm.prototype.get=function(tmp){
		x=[tmp.showX(),tmp.showY(),tmp.showX2(),tmp.showY2()];
		if(code[x.join('@')]){
			return code[x.join('@')];
		}else{
			throw "erro";
			return;
		}
	}
	
	var sechash=$("[name='sechash']");
	if(sechash.length>0){
		var v=sechash.val();
		$('#seccode'+v+'_menu').css({'position':'static','display':''});
			
		(function(){
			var p=$("#seccode_"+v+" .vm");
			if(p.length!=0){
				clearInterval(window.secST['code_' + v]);
				var yz=new $.yzm(p[0],4);
				p.bind('load',function (){
					document.getElementsByName("seccodeverify")[0].value='';
					try{
						document.getElementsByName("seccodeverify")[0].value=yz.load().removeBG().cut();
					}catch (e) {
						erro();
						return;
					}
					if(__f==1&&document.getElementsByName("seccodeverify")[0].value!=''){
						pref.setValue('cookie'+__g,gw.cookie.get('.139life.com','/'));
						pref.setValue('sechash'+__g,'&sechash='+v+'&seccodeverify='+document.getElementsByName("seccodeverify")[0].value);
						__g++;
						allMark();
					}
				});
			}else{
				setTimeout(arguments.callee,1000);
			}
		})();
	}

	//send
	function send(){
		var fid=window.fid,tid=window.tid,i=0;
		var message=document.getElementById('fastpostmessage').value;
		var urls=['http://www.139life.com/forum.php?mod=post&action=reply&fid='+fid+'&tid='+tid+'&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1'
		//,'http://www.139life.com/forum.php?mod=post&action=reply&fid=162&tid=270168&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1'
		//,'http://www.139life.com/forum.php?mod=post&action=reply&fid=121&tid=273117&extra=&replysubmit=yes&infloat=yes&handlekey=fastpost&inajax=1'
		];
		if(message==''){
			alert('no message!');
			return ;
		}
		
		for(i=0;i<urls.length;i++){
			ob.add('request',urls[i],'get','Cookie','wgu');
			ob.add('request',urls[i],'set','wgu',null);
		}
		
		(function (){
			_allMark();
			setTimeout(arguments.callee,3*40*1000);
		})();
		
		for(i=0;i<useInfo.length;i++){
			if(typeof useInfo[i][4]=="undefined"){
				continue;
			}
			(function (pref,i,req,url,message){
				function p(){
					req.open('POST', url, true); 
					req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					req.setRequestHeader('wgu',pref.getValue('cookie'+i));
					req.send(pref.getValue('fromhash'+i)+'&subject=&usesig=1&addfeed=1&message='+message+pref.getValue('sechash'+i));
				}
				req.onreadystatechange = function () {
					if (req.readyState == 4) { 						
						setTimeout(p,31000);							
					}
				};
				return p
			})(pref,i,new XMLHttpRequest(),urls[useInfo[i][4]],parseurl(useInfo[i][5]||message))();		
		}
		console.log('load');
	}

	function apInput(parent,value,fn){
		var input = document.createElement("input"); 
		input.type= "button";
		input.value= value;
		input.addEventListener("click", fn, false);
		parent.appendChild(input);
	}
	__f=0;
	__g=0;
	function allMark(){
		if(__g<useInfo.length){
			if(typeof useInfo[__g][4]=="undefined"){
				__g++;
				allMark();
				return;
			}
			gw.cookie.remove('.139life.com','/');
			gw.cookie.add('.139life.com','/',pref.getValue('cookie'+__g));
			updateseccode(document.getElementsByName("sechash")[0].value);
		}else{
			__f=0;
			__g=0;
			console.log('allMark');
		}
	}

	function _allMark(){
		__g=0;
		__f=1;
		allMark();
	}

	var fn=[_allMark,send],p=$('.ptm');
	fn.forEach(
		function(v,i){
			$('<input type="button" value="'+v.name+'">').appendTo(p).bind('click',v);
		}
	);
})(jQuery);